package com.lobstar.xrpc.server.handler;

import com.lobstar.xrpc.common.handler.SMessageDecoder;
import com.lobstar.xrpc.common.handler.SMessageEncoder;
import com.lobstar.xrpc.common.po.Const;
import com.lobstar.xrpc.server.main.MainServer;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;

public class ServerInitializer extends ChannelInitializer<SocketChannel>{
	private MainServer server;
	public ServerInitializer(MainServer server) {
		this.server = server;
	}
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new DelimiterBasedFrameDecoder(Const.MESSAGE_MAX_LENGTH, Unpooled.copiedBuffer(Const.DELIMITER.getBytes("UTF-8"))));
		pipeline.addLast(new SMessageEncoder());
		pipeline.addLast(new SMessageDecoder());
		pipeline.addLast("handler",new ServerHandler(server));
	}
}
